{include file="common/pc/header" title="企业详情" /}

<style>
    #enterprises-detail img {
        width: 100px;
        height: 100px;
    }

    #modules-table img {
        width: 60px;
        height: 60px;
    }

    #modules-table a.layui-btn {
        width: 70px;
        display: block;
        margin-right: 0px;
    }

    #modules-table .not-active {
        color: #dad5d5;
    }

    #modules-table .not-active .layui-badge,
    #modules-table .not-active .layui-btn {
        background-color: #cccccc !important;
    }

    #modules-table .not-active img {
        -webkit-filter: grayscale(1); /* Webkit */
        filter: gray; /* IE6-9 */
        filter: grayscale(1); /* W3C */
    }
</style>

<script type="text/javascript" src="__JS__/date/date-zh-CN.js"></script>

<div id="enterprises-detail">
    <table class="layui-table">
        <tr>
            <td rowspan="2" width="100">
                <img src="{$enterprise.logo}" onerror="this.src='__IMAGES__/enterprise.png';"/>
            </td>
            <td>企业名称: {$enterprise.title}</td>
            <td rowspan="2" valign="top">
                企业描述: <br/>
                {$enterprise.description}
            </td>
        </tr>
        <tr>
            <td>
                联系人：{$enterprise.contacter}
                <br/>
                联系电话: {$enterprise.phone}
            </td>
        </tr>
    </table>
    <table class="layui-table" id="modules-table">
        <colgroup>
            <col width="40">
            <col width="60">
            <col width="120">
            <col width="80">
            <col width="200">
            <col width="100">
            <col width="150">
            <col width="100">
            <col width="250">
            <col width="100">
        </colgroup>
        <thead>
        <tr>
            <th>Id</th>
            <th>Logo</th>
            <th>模块标识</th>
            <th>版本</th>
            <th>模块名称</th>
            <th>状态</th>
            <th>授权到期时间</th>
            <th>微应用</th>
            <th>模块作者</th>
            <th>管理</th>
        </tr>
        </thead>
        <tbody>
        {volist name="modules" id="module"}
        {if condition="$module.status != 'active'"}
        <tr class="not-active" title="此模块尚未安装或者已停用">
            {else/}
        <tr>
            {/if}
            <td>{$module.id}</td>
            <td>
                <img src="/{$module.name}/logo.png" onerror="this.src='__IMAGES__/module.png';"/>
            </td>
            <td>
                {$module.name}
            </td>
            <td>
                {$module.version}
            </td>
            <td>
                {$module.title}
            </td>
            <td>
                {if condition="$module['enterpriseModule']['status'] == 'normal'"}
                <span class="layui-badge layui-bg-green">已授权</span>
                {elseif condition="$module['enterpriseModule']['status'] == 'expired'"}
                <span class="layui-badge layui-bg-orange">授权到期</span>
                {else /}
                <span class="layui-badge layui-bg-orange">未授权</span>
                {/if}
                {if condition="$module['status'] != 'active'"}
                <span class="layui-badge layui-bg-orange">未启用</span>
                {/if}
            </td>
            <td>
                {php}
                echo empty($module['enterpriseModule']['expiry']) ? '': date('Y-m-d',
                $module['enterpriseModule']['expiry']);
                {/php}
            </td>
            <td>
                {php}
                $agentId = $module['enterpriseModule']['agent_id'];
                {/php}
                {$agentId}
            </td>
            <td>
                {$module.author}
                <br/>
                <a href="{$module.url}" target="_blank">{$module.url}</a>
            </td>
            <td align="center" data_module='{php}echo json_encode($module);{/php}'
                data_enterpriseModule='{php}echo json_encode($module["enterpriseModule"]);{/php}'>
                <a class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:;"
                   onclick="openModuleDetailTab('{$module.title}','{$module.name}')">模块详情</a>
                <a class="layui-btn layui-btn-sm layui-btn-success" href="javascript:;"
                   onclick="grantManage(this)">授权管理</a>
            </td>
        </tr>
        {/volist}
        </tbody>
    </table>
</div>

<script>
    /**
     * 模块详情
     */
    function openModuleDetailTab(title, moduleName, initAction) {
        var link = "/core/site.module/detail/" + moduleName + "?init=" + initAction;
        layer.open({
            type: 2,
            title: title,
            shadeClose: true,
            shade: false,
            maxmin: false,
            area: ['80vw', '80vh'],
            resize: true,
            content: link
        });

    }

    /**
     * 撤销赋权
     */
    function revoke(module, callback) {
        var content = "撤销后，企业【{$enterprise.title}】将<b class='layui-bg-red'>无法使用</b>模块【" + module.title + "】，确定继续撤销？";
        layer.confirm(content, {title: '确认'}, function (cIndex) {
            g.loading();
            $.post("/core/site.enterprise/revoke", {
                moduleName: module.name,
                eid: '{$enterprise.id}'
            }, function (ret) {
                g.loading(false);
                callback(ret);
                if (ret.code == 1) {
                    layer.msg("撤销成功，页面刷新中");
                    document.location.reload();
                } else {
                    layer.alert(ret.msg);
                }
            }, "json");
            layer.close(cIndex);
        });
    }

    /**
     * 授权使用
     */
    function grant(module, expiry, callback) {
        var content = "确认将模块【" + module.title + "】授权给企业【{$enterprise.title}】使用？使用期限：<b class='layui-bg-red'>" + (expiry || "无限制") + "</b>";
        layer.confirm(content, {title: '确认'}, function (cIndex) {
            g.loading();
            $.post("/core/site.enterprise/grant", {
                moduleName: module.name,
                eid: '{$enterprise.id}',
                expiry: expiry
            }, function (ret) {
                g.loading(false);
                callback(ret);
                if (ret.code == 1) {
                    layer.msg("授权成功，页面刷新中");
                    document.location.reload();
                } else {
                    layer.alert(ret.msg);
                }
            }, "json");
            layer.close(cIndex);
        });
    }

    /**
     * 授权管理
     *
     * @param that
     */
    function grantManage(that) {
        var module = $(that).parent().attr("data_module");
        var enterpriseModule = $(that).parent().attr("data_enterpriseModule");
        module = eval("(" + module + ")");
        enterpriseModule = eval("(" + enterpriseModule + ")");

        var opt = {
            type: 1,
            title: '授权管理',
            content: $("#grantManageDlg"),
            area: ['450px']
        };
        if (enterpriseModule == false || enterpriseModule.status != 'normal') {
            if (enterpriseModule == false) {
                enterpriseModule = {};
            }
            opt.btn = ['确定', '取消'];
            opt.yes = function (index, layero) {
                var expiry = $("#grantManageDlg input[name='expired']").val();
                grant(module, expiry, function () {
                    layer.close(index);
                });
                return true;
            };
        } else {
            opt.btn = ['确定', '撤销授权', '取消'];
            opt.yes = function (index, layero) {
                var expiry = $("#grantManageDlg input[name='expired']").val();
                grant(module, expiry, function () {
                    layer.close(index);
                });
                return true;
            };
            opt.btn2 = function (index, layero) {
                revoke(module, function () {
                    layer.close(index);
                });
                return false;
            }
        }

        // 显示对话框
        var expiry = enterpriseModule.expiry ? new Date(enterpriseModule.expiry * 1000).toString('yyyy-MM-dd') : "";
        $("#grantManageDlg input[name='moduleName']").val(module.name);
        $("#grantManageDlg input[name='moduleTitle']").val(module.title);
        $("#grantManageDlg input[name='expired']").val(expiry);
        layer.open(opt);
    }
</script>

<div id="grantManageDlg" style="display: none;">
    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">企业名称：</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input readonly" readonly value="{$enterprise.title}"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">模块名称：</label>
            <div class="layui-input-block">
                <input type="hidden" value="" name="moduleName"/>
                <input type="text" class="layui-input readonly" readonly value="" name="moduleTitle"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">授权到期：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="expired"/>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['laydate', 'form'], function () {
        window.laydate = layui.laydate;
        var form = layui.form;
        $("#grantManageDlg input[name='expired']").each(function () {
            laydate.render({
                elem: this,
                format: 'yyyy-MM-dd'
            });
        });
    });
</script>
{include file="common/pc/footer"/}